<?php


include '../utils/stock.php';
include '../utils/evaluator.php';
require('../utils/fpdf17/fpdf.php');
session_start() ;


session_start();
$stock = new stock;
$stock->get_stock_quote($_SESSION["evaluator"]->ticker);

// Begin configuration

$textColour = array( 0, 0, 0 );
$headerColour = array( 100, 100, 100 );
$tableHeaderTopTextColour = array( 275, 275, 275 );
$tableHeaderTopFillColour = array( 125, 152, 179 );
$tableHeaderTopProductTextColour = array( 0, 0, 0 );
$tableHeaderTopProductFillColour = array( 143, 173, 204 );
$tableHeaderLeftTextColour = array( 99, 42, 57 );
$tableHeaderLeftFillColour = array( 184, 207, 229 );
$tableBorderColour = array( 50, 50, 50 );
$tableRowFillColour = array( 213, 170, 170 );
$reportName = "BRIKS: STOCK EVALUATION";
$reportNameYPos = 30;
$reportSubName = $_SESSION["evaluator"]->name;
$reportSubNameYPos = 120;
$logoFile = "../utils/briks.png";
$logoXPos = 50;
$logoYPos = 78;
$logoWidth = 110;
$columnLabels = array( "Book Value" , "BV Growth" );
$columnLabels2 = array( "EPS" , "EPS Growth" );
$rowLabels = array("2003","2004","2005","2006","2007","2008","2009","2010","2011","2012");
$chartXPos = 20;
$chartYPos = 250;
$chartWidth = 160;
$chartHeight = 80;
$chartXLabel = "Year";
$chartYLabel = "Book Value";
$chartYLabel2 = "EPS";
$chartYStep = 20000;

$chartColours = array(
                  array( 275, 100, 100 ),
                  array( 100, 275, 100 ),
                  array( 100, 100, 275 ),
                  array( 275, 275, 100 ),
                );

$data = array(
           array( $_SESSION["evaluator"]->bookvalue2003,0),
           array( $_SESSION["evaluator"]->bookvalue2004,$_SESSION["evaluator"]->bvg2004),
           array( $_SESSION["evaluator"]->bookvalue2005,$_SESSION["evaluator"]->bvg2005),
           array( $_SESSION["evaluator"]->bookvalue2006,$_SESSION["evaluator"]->bvg2006),
           array( $_SESSION["evaluator"]->bookvalue2007,$_SESSION["evaluator"]->bvg2007),
           array( $_SESSION["evaluator"]->bookvalue2008,$_SESSION["evaluator"]->bvg2008),
           array( $_SESSION["evaluator"]->bookvalue2009,$_SESSION["evaluator"]->bvg2009),
           array( $_SESSION["evaluator"]->bookvalue2010,$_SESSION["evaluator"]->bvg2010),
           array( $_SESSION["evaluator"]->bookvalue2011,$_SESSION["evaluator"]->bvg2011),
           array( $_SESSION["evaluator"]->bookvalue2012,$_SESSION["evaluator"]->bvg2012),
        );
 $data2 = array(
           array( $_SESSION["evaluator"]->eps2003,0),
           array( $_SESSION["evaluator"]->eps2004,$_SESSION["evaluator"]->epsg2004),
           array( $_SESSION["evaluator"]->eps2005,$_SESSION["evaluator"]->epsg2005),
           array( $_SESSION["evaluator"]->eps2006,$_SESSION["evaluator"]->epsg2006),
           array( $_SESSION["evaluator"]->eps2007,$_SESSION["evaluator"]->epsg2007),
           array( $_SESSION["evaluator"]->eps2008,$_SESSION["evaluator"]->epsg2008),
           array( $_SESSION["evaluator"]->eps2009,$_SESSION["evaluator"]->epsg2009),
           array( $_SESSION["evaluator"]->eps2010,$_SESSION["evaluator"]->epsg2010),
           array( $_SESSION["evaluator"]->eps2011,$_SESSION["evaluator"]->epsg2011),
           array( $_SESSION["evaluator"]->eps2012,$_SESSION["evaluator"]->epsg2012),
        );
        

// End configuration


/**
  Create the title page
**/

$pdf = new FPDF( 'P', 'mm', 'A4' );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->AddPage();

// Logo
$pdf->Image( $logoFile, $logoXPos, $logoYPos, $logoWidth );

// Report Name
$pdf->SetFont( 'Arial', 'B', 24 );
$pdf->Ln( $reportNameYPos );
$pdf->Cell( 0, 15, $reportName, 0, 0, 'C' );
$pdf->Ln( $reportSubNameYPos );
$pdf->Cell( 0, 15, $reportSubName, 0, 0, 'C' );


/**
  Create the page header, main heading, and intro text
**/

$pdf->AddPage();
$pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] );
$pdf->SetFont( 'Arial', '', 17 );
$pdf->Cell( 0, 15, $reportSubName, 0, 0, 'C' );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetFont( 'Arial', '', 20 );
$pdf->Write( 19, "Key figures" );
$pdf->Ln( 16 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write(6,'Name: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION['evaluator']->name);
$pdf->Ln( 6);
$pdf->Write(6,'Ticker: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION['evaluator']->ticker);
$pdf->Ln( 6);
$pdf->Write(6,'Stock exchange: ');
$pdf->SetX(75);
$pdf->Write(6,$stock->se);
$pdf->Ln( 6);
$pdf->Write(6,'Price: ');
$pdf->SetX(75);
$pdf->Write(6,$stock->last);
$pdf->Ln( 6);
$pdf->Write(6,'52-Week High: ');
$pdf->SetX(75);
$pdf->Write(6,$stock->wh);
$pdf->Ln( 6);
$pdf->Write(6,'52-Week Low: ');
$pdf->SetX(75);
$pdf->Write(6,$stock->wl);
$pdf->Ln( 6);
$pdf->Write(6,'Dividend/Share: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION['evaluator']->anualised_dividend);
$pdf->Ln( 6);
$pdf->Write(6,'EPS: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION['evaluator']->getEPS());
$pdf->Ln( 6);
$pdf->Write(6,'EBITDA: ');
$pdf->SetX(75);
$pdf->Write(6,$stock->ebitda);
$pdf->Ln( 6);
$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'P/E: ');
$pdf->SetX(75);
$pb = $_SESSION['evaluator']->getPB();
$pe = $_SESSION['evaluator']->getPE();
$colorpe = addColorSmaller($pe,15);
$per = $colorpe[0];
$peg = $colorpe[1];
$pdf->SetTextColor($per,$peg,0);
$pdf->Write(6,$pe);
$pdf->SetTextColor(0,0,0);
$pdf->Ln( 6);
$pdf->Write(6,'P/B: ');
$pdf->SetX(75);
$colorpb = addColorSmaller($pb,1.5);
$pbr = $colorpb[0];
$pbg = $colorpb[1];
$pdf->SetTextColor($pbr,$pbg,0);
$pdf->Write(6,$pb);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Ln( 16);


$pdf->AddPage();
$pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] );
$pdf->SetFont( 'Arial', '', 17 );
$pdf->Cell( 0, 15, $reportSubName, 0, 0, 'C' );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetFont( 'Arial', '', 20 );
$pdf->Write( 19, "Financial Condition" );
$pdf->Ln( 16 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write(6,'Total debt: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->total_debt);
$pdf->Ln( 6);
$pdf->Write(6,'Total equity: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->total_equity);
$pdf->Ln( 6);

$pdf->Write(6,'Total current assets: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->current_assets);
$pdf->Ln( 6);
$pdf->Write(6,'Total assets: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->total_assets);
$pdf->Ln( 6);
$pdf->Write(6,'Total current liabilities: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->current_liabilities);
$pdf->Ln( 6);
$pdf->Write(6,'Total  liabilities: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->total_liabilities);
$pdf->Ln( 6);
$pdf->Write(6,'EBIT: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION['evaluator']->ebit);
$pdf->Ln( 6);
$pdf->Write(6,'Net fixed assets: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION['evaluator']->net_fixed_assets);
$pdf->Ln( 6);





$pdf->Ln( 6);
$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Debt/Equity: ');
$pdf->SetX(75);

$colorde = addColorSmaller($_SESSION["evaluator"]->getDebtOnEquity(),0.5);
$der = $colorde[0];
$deg = $colorde[1];
$pdf->SetTextColor($der,$deg,0);
$debtoneq = round($_SESSION["evaluator"]->getDebtOnEquity()*100,2);
$pdf->Write(6,$debtoneq." %");
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(<50%)");
$pdf->Ln( 6);

$pdf->Write(6,'Current ratio: ');
$pdf->SetX(75);
$colorcr = addColorBigger($_SESSION['evaluator']->getCurrentRatio(),1);
$crr = $colorcr[0];
$crg = $colorcr[1];
$pdf->SetTextColor($crr,$crg,0);
$pdf->Write(6,round($_SESSION["evaluator"]->getCurrentRatio(),3));
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>1)");
$pdf->SetFont( 'Arial', '', 12);
$pdf->Ln( 6);


$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Financial indepedance:');
$pdf->SetX(75);

$colorfi = addColorBigger($_SESSION['evaluator']->getFinancialIndependence(),0.333);
$fir = $colorfi[0];
$fig = $colorfi[1];
$pdf->SetTextColor($fir,$fig,0);
$pdf->Write(6,$_SESSION["evaluator"]->getFinancialIndependence());
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>33%)");
$pdf->Ln( 6);




$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Constricted f.i.: ');
$pdf->SetX(75);

$colorcfi = addColorBigger($_SESSION["evaluator"]->getConstrictedFinancialIndependence(),0.5);
$cfir = $colorcfi[0];
$cfig = $colorcfi[1];
$pdf->SetTextColor($cfir,$cfig,0);
$pdf->Write(6,$_SESSION["evaluator"]->getConstrictedFinancialIndependence());
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>50%)");
$pdf->SetFont( 'Arial', '', 12);
$pdf->Ln( 6);
$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Working capital: ');
$wc = $_SESSION['evaluator']->getWorkingCapital();
$pdf->SetX(75);
$colorwc = addColorBigger($wc,0);
$wcr = $colorwc[0];
$wcg = $colorwc[1];
$pdf->SetTextColor($wcr,$wcg,0);
$pdf->Write(6,$wc);
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>0)");
$pdf->SetFont( 'Arial', '', 12);
$pdf->Ln( 6);


$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetFont( 'Arial', '', 20 );
$pdf->Write( 19, "Valuation" );
$pdf->Ln( 16 );
$pdf->SetFont( 'Arial', '', 12);


$pdf->Write(6,'Present book value: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->bookvalue2012);
$pdf->Ln( 6);
$pdf->Write(6,'Net income: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->net_income);
$pdf->Ln( 6);
$pdf->Write(6,'Earnings per share: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->getEPS());
$pdf->Ln( 6);
$pdf->Write(6,'# shares: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->nb_shares);
$pdf->Ln( 6);
$pdf->Write(6,'Dividend: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->anualised_dividend);
$pdf->Ln( 6);

$pdf->Write(6,'Federal note: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->federal_note);
$pdf->Ln( 6);
$pdf->Ln( 6);

$pdf->Write(6,'Average book value growth: ');
$pdf->SetX(75);
$avg = round($_SESSION["evaluator"]->average_growth*100,2);
$pdf->Write(6,$avg." %");
$pdf->Ln( 6);
$pdf->Write(6,'Future book value: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->getFBV());
$pdf->Ln( 6);
$pdf->Write(6,'Mod. book value growth: ');
$pdf->SetX(75);
$pdf->Write(6,round($_SESSION["evaluator"]->average_growth_modified*100,2)." %");
$pdf->Ln( 6);
$pdf->Write(6,'Mod. Future book value: ');
$pdf->SetX(75);
$pdf->Write(6,$_SESSION["evaluator"]->getFBVp($_SESSION["evaluator"]->bookvalue2012,$_SESSION["evaluator"]->average_growth_modified));

$pdf->Ln( 6);
$pdf->Ln( 6);

$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'EPS on Market Price: ');
$pdf->SetX(75);
$colorepsp = addColorBigger($_SESSION["evaluator"]->getEPSonPrice(),0.07);
$epr = $colorepsp[0];
$epg = $colorepsp[1];
$pdf->SetTextColor($epr,$epg,0);
$epsonprice = round($_SESSION["evaluator"]->getEPSonPrice()*100,2);
$pdf->Write(6,$epsonprice." %");
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>7%)");
$pdf->Ln( 6);
$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Avg EPS on Market Price: ');
$pdf->SetX(75);
$coloraepsp = addColorBigger($_SESSION["evaluator"]->getAVGEPSonPrice(),0.07);
$aepr = $coloraepsp[0];
$aepg = $coloraepsp[1];
$pdf->SetTextColor($aepr,$aepg,0);
$aepsonprice = round($_SESSION["evaluator"]->getAVGEPSonPrice()*100,2);
$pdf->Write(6,$aepsonprice." %");
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>7%)");
$pdf->Ln( 6);
$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Dividend on EPS: ');
$pdf->SetX(75);
$colordiveps = addColorSmaller($_SESSION["evaluator"]->getDividendOnEPS(),0.9);
$der = $colordiveps[0];
$deg = $colordiveps[1];
$pdf->SetTextColor($der,$deg,0);
$divoneps = round($_SESSION["evaluator"]->getDividendOnEPS()*100,2);
$pdf->Write(6,$divoneps." %");
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(<90%)");
$pdf->Ln( 6);
$pdf->Write(6,'Return on equity: ');
$pdf->SetX(75);
$colorroe = addColorBigger($_SESSION["evaluator"]->getROE(),0.15);
$roer = $colorroe[0];
$roeg = $colorroe[1];
$pdf->SetTextColor($roer,$roeg,0);
$roe = round($_SESSION["evaluator"]->getROE()*100,2);
$pdf->Write(6,$roe." %");
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>15%)");
$pdf->Ln( 6);

$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Return on capital employed: ');
$roce = $_SESSION['evaluator']->getReturnOnCapitalEmployed();
$pdf->SetX(75);
$colorroce = addColorBigger($roce,0);
$rocer = $colorroce[0];
$roceg = $colorroce[1];
$roce = round($roce*100,2);
$pdf->SetTextColor($rocer,$roceg,0);
$pdf->Write(6,$roce." %");
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(>0%)");
$pdf->SetFont( 'Arial', '', 12);
$pdf->Ln( 6);
$pdf->Ln( 6);

$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Intrinsic value: ');
$pdf->SetX(75);
$coloriv = addColorBigger($_SESSION["evaluator"]->getIntrinsicValue(),$_SESSION["evaluator"]->price);
$ivr = $coloriv[0];
$ivg = $coloriv[1];
$pdf->SetTextColor($ivr,$ivg,0);
$pdf->Write(6,round($_SESSION["evaluator"]->getIntrinsicValue(),2));
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(> Market price)");
$pdf->Ln( 6);
$pdf->SetFont( 'Arial', 'B', 12);
$pdf->Write(6,'Mod. Intrinsic value: ');
$pdf->SetX(75);
$ivmod = $_SESSION["evaluator"]->getIntrinsicValuep($_SESSION["evaluator"]->bookvalue2012, $_SESSION["evaluator"]->average_growth_modified, $_SESSION["evaluator"]->federal_note, $_SESSION["evaluator"]->anualised_dividend);
$coloriv = addColorBigger($ivmod,$_SESSION["evaluator"]->price);
$ivr = $coloriv[0];
$ivg = $coloriv[1];
$pdf->SetTextColor($ivr,$ivg,0);
$pdf->Write(6,round($ivmod,2));
$pdf->SetTextColor(0,0,0);
$pdf->SetX(150);
$pdf->Write(6,"(> Market price)");
$pdf->Ln( 6);
//$pdf->Write(6,'Expected anual return: ');
//$pdf->SetX(75);
//$pdf->Write(6,$_SESSION["expectedreturn"]." %");
//$pdf->SetFont( 'Arial', '', 12);

$pdf->AddPage();
$pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] );
$pdf->SetFont( 'Arial', '', 17 );
$pdf->Cell( 0, 15, $reportSubName, 0, 0, 'C' );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetFont( 'Arial', '', 20 );
$pdf->Write( 1, "Book Value Visualisation" );
$pdf->Ln( 1 );
$pdf->SetFont( 'Arial', '', 12);
/**
  Create the table
**/

$pdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
$pdf->Ln( 15 );

// Create the table header row
$pdf->SetFont( 'Arial', 'B', 15 );

// "PRODUCT" cell
$pdf->SetTextColor( $tableHeaderTopProductTextColour[0], $tableHeaderTopProductTextColour[1], $tableHeaderTopProductTextColour[2] );
$pdf->SetFillColor( $tableHeaderTopProductFillColour[0], $tableHeaderTopProductFillColour[1], $tableHeaderTopProductFillColour[2] );
$pdf->Cell( 46, 12, " Year", 1, 0, 'L', true );

// Remaining header cells
$pdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
$pdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );

for ( $i=0; $i<count($columnLabels); $i++ ) {
  $pdf->Cell( 36, 12, $columnLabels[$i], 1, 0, 'C', true );
}

$pdf->Ln( 12 );

// Create the table data rows

$fill = false;
$row = 0;

foreach ( $data as $dataRow ) {

  // Create the left header cell
  $pdf->SetFont( 'Arial', 'B', 15 );
  $pdf->SetTextColor( $tableHeaderLeftTextColour[0], $tableHeaderLeftTextColour[1], $tableHeaderLeftTextColour[2] );
  $pdf->SetFillColor( $tableHeaderLeftFillColour[0], $tableHeaderLeftFillColour[1], $tableHeaderLeftFillColour[2] );
  $pdf->Cell( 46, 12, " " . $rowLabels[$row], 1, 0, 'L', $fill );

  // Create the data cells
  $pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
  $pdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
  $pdf->SetFont( 'Arial', '', 15 );

  for ( $i=0; $i<count($columnLabels); $i++ ) {
    $pdf->Cell( 36, 12, ( number_format( $dataRow[$i],3 ) ), 1, 0, 'C', $fill );
  }

  $row++;
  $fill = !$fill;
  $pdf->Ln( 12 );
}


/***
  Create the chart
***/

// Compute the X scale
$xScale = count($rowLabels) / ( $chartWidth - 40 );

// Compute the Y scale

$maxTotal = 0;

foreach ( $data as $dataRow ) {
  $totalSales = $dataRow[0];
  //foreach ( $dataRow as $dataCell ) $totalSales = $dataCell;
  $maxTotal = ( $totalSales > $maxTotal ) ? $totalSales : $maxTotal;
}

$yScale = $maxTotal / $chartHeight;

// Compute the bar width
$barWidth = ( 1 / $xScale ) / 1.5;

// Add the axes:

$pdf->SetFont( 'Arial', '', 10 );

// X axis
$pdf->Line( $chartXPos + 30, $chartYPos, $chartXPos + $chartWidth, $chartYPos );

for ( $i=0; $i < count( $rowLabels ); $i++ ) {
  $pdf->SetXY( $chartXPos + 40 +  $i / $xScale, $chartYPos );
  $pdf->Cell( $barWidth, 10, $rowLabels[$i], 0, 0, 'C' );
}

// Y axis
$pdf->Line( $chartXPos + 30, $chartYPos, $chartXPos + 30, $chartYPos - $chartHeight - 8 );

for ( $i=0; $i <= $maxTotal; $i += $chartYStep ) {
  $pdf->SetXY( $chartXPos + 7, $chartYPos - 5 - $i / $yScale );
  $pdf->Cell( 20, 10,  number_format( $i ), 0, 0, 'R' );
  $pdf->Line( $chartXPos + 28, $chartYPos - $i / $yScale, $chartXPos + 30, $chartYPos - $i / $yScale );
}

// Add the axis labels
$pdf->SetFont( 'Arial', 'B', 12 );
$pdf->SetXY( $chartWidth / 2 + 20, $chartYPos + 8 );
$pdf->Cell( 30, 10, $chartXLabel, 0, 0, 'C' );
$pdf->SetXY( $chartXPos + 7, $chartYPos - $chartHeight - 12 );
$pdf->Cell( 20, 10, $chartYLabel, 0, 0, 'R' );

// Create the bars
$xPos = $chartXPos + 40;
$bar = 0;

foreach ( $data as $dataRow ) {

  // Total up the sales figures for this product
  $totalSales = $dataRow[0];
  //foreach ( $dataRow as $dataCell ) $totalSales += $dataCell;

  // Create the bar
  $colourIndex = $bar % count( $chartColours );
  $pdf->SetFillColor( $chartColours[$colourIndex][0], $chartColours[$colourIndex][1], $chartColours[$colourIndex][2] );
  $pdf->Rect( $xPos, $chartYPos - ( $totalSales / $yScale ), $barWidth, $totalSales / $yScale, 'DF' );
  $xPos += ( 1 / $xScale );
  $bar++;
}


// EARNINGS


$pdf->AddPage();
$pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] );
$pdf->SetFont( 'Arial', '', 17 );
$pdf->Cell( 0, 15, $reportSubName, 0, 0, 'C' );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetFont( 'Arial', '', 20 );
$pdf->Write( 1, "EPS Visualisation" );
$pdf->Ln( 1 );
$pdf->SetFont( 'Arial', '', 12);
/**
  Create the table
**/

$pdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
$pdf->Ln( 15 );

// Create the table header row
$pdf->SetFont( 'Arial', 'B', 15 );

// "PRODUCT" cell
$pdf->SetTextColor( $tableHeaderTopProductTextColour[0], $tableHeaderTopProductTextColour[1], $tableHeaderTopProductTextColour[2] );
$pdf->SetFillColor( $tableHeaderTopProductFillColour[0], $tableHeaderTopProductFillColour[1], $tableHeaderTopProductFillColour[2] );
$pdf->Cell( 46, 12, " Year", 1, 0, 'L', true );

// Remaining header cells
$pdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
$pdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );

for ( $i=0; $i<count($columnLabels2); $i++ ) {
  $pdf->Cell( 36, 12, $columnLabels2[$i], 1, 0, 'C', true );
}

$pdf->Ln( 12 );

// Create the table data rows

$fill = false;
$row = 0;

foreach ( $data2 as $dataRow ) {

  // Create the left header cell
  $pdf->SetFont( 'Arial', 'B', 15 );
  $pdf->SetTextColor( $tableHeaderLeftTextColour[0], $tableHeaderLeftTextColour[1], $tableHeaderLeftTextColour[2] );
  $pdf->SetFillColor( $tableHeaderLeftFillColour[0], $tableHeaderLeftFillColour[1], $tableHeaderLeftFillColour[2] );
  $pdf->Cell( 46, 12, " " . $rowLabels[$row], 1, 0, 'L', $fill );

  // Create the data cells
  $pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
  $pdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
  $pdf->SetFont( 'Arial', '', 15 );

  for ( $i=0; $i<count($columnLabels2); $i++ ) {
    $pdf->Cell( 36, 12, ( number_format( $dataRow[$i],3 ) ), 1, 0, 'C', $fill );
  }

  $row++;
  $fill = !$fill;
  $pdf->Ln( 12 );
}


/***
  Create the chart
***/

// Compute the X scale
$xScale = count($rowLabels) / ( $chartWidth - 40 );

// Compute the Y scale

$maxTotal = 0;

foreach ( $data2 as $dataRow ) {
  $totalSales = $dataRow[0];
  //foreach ( $dataRow as $dataCell ) $totalSales = $dataCell;
  $maxTotal = ( $totalSales > $maxTotal ) ? $totalSales : $maxTotal;
}

$yScale = $maxTotal / $chartHeight;

// Compute the bar width
$barWidth = ( 1 / $xScale ) / 1.5;

// Add the axes:

$pdf->SetFont( 'Arial', '', 10 );

// X axis
$pdf->Line( $chartXPos + 30, $chartYPos, $chartXPos + $chartWidth, $chartYPos );

for ( $i=0; $i < count( $rowLabels ); $i++ ) {
  $pdf->SetXY( $chartXPos + 40 +  $i / $xScale, $chartYPos );
  $pdf->Cell( $barWidth, 10, $rowLabels[$i], 0, 0, 'C' );
}

// Y axis
$pdf->Line( $chartXPos + 30, $chartYPos, $chartXPos + 30, $chartYPos - $chartHeight - 8 );

for ( $i=0; $i <= $maxTotal; $i += $chartYStep ) {
  $pdf->SetXY( $chartXPos + 7, $chartYPos - 5 - $i / $yScale );
  $pdf->Cell( 20, 10,  number_format( $i ), 0, 0, 'R' );
  $pdf->Line( $chartXPos + 28, $chartYPos - $i / $yScale, $chartXPos + 30, $chartYPos - $i / $yScale );
}

// Add the axis labels
$pdf->SetFont( 'Arial', 'B', 12 );
$pdf->SetXY( $chartWidth / 2 + 20, $chartYPos + 8 );
$pdf->Cell( 30, 10, $chartXLabel, 0, 0, 'C' );
$pdf->SetXY( $chartXPos + 7, $chartYPos - $chartHeight - 12 );
$pdf->Cell( 20, 10, $chartYLabel2, 0, 0, 'R' );

// Create the bars
$xPos = $chartXPos + 40;
$bar = 0;

foreach ( $data2 as $dataRow ) {

  // Total up the sales figures for this product
  $totalSales = $dataRow[0];
  //foreach ( $dataRow as $dataCell ) $totalSales += $dataCell;

  // Create the bar
  $colourIndex = $bar % count( $chartColours );
  $pdf->SetFillColor( $chartColours[$colourIndex][0], $chartColours[$colourIndex][1], $chartColours[$colourIndex][2] );
  $pdf->Rect( $xPos, $chartYPos - ( $totalSales / $yScale ), $barWidth, $totalSales / $yScale, 'DF' );
  $xPos += ( 1 / $xScale );
  $bar++;
}
$pdf->AddPage();
$pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] );
$pdf->SetFont( 'Arial', '', 17 );
$pdf->Cell( 0, 15, $reportSubName, 0, 0, 'C' );
$pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
$pdf->SetFont( 'Arial', '', 20 );
$pdf->Write( 19, "Valuation" );
$pdf->Ln( 16 );


/***
  Serve the PDF
***/

function addColorSmaller($number, $reference){
	if($number <= $reference) 
	{
		$array = array(0,128,0,);
	}
	else 
	{
		$array = array(255,0,0,);
	}
	
	return $array;
}
function addColorBigger($number, $reference){
	if($number >= $reference) 
	{
		$array = array(0,128,0,);
	}
	else 
	{
		$array = array(255,0,0,);
	}
	
	return $array;
}

$pdf->Output( "report.pdf", "I" );


?>

